//
//  ZXJVerityCodeLoginViewController.m
//  academic_records
//
//  Created by zhang bozhao on 2020/8/5.
//  Copyright © 2020 zhang bozhao. All rights reserved.
//

#import "ZXJVerityCodeLoginViewController.h"
#import "ZXJInputVerityCodeViewController.h"
#import "LoginRequest.h"
#import "WKWebViewController.h"

@interface ZXJVerityCodeLoginViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIView *phoneView;

@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UILabel *textLab;
@property (weak, nonatomic) IBOutlet UIButton *actionBtn;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (nonatomic, strong)NSString *codeMode;
@property(nonatomic,strong)WKWebViewController *webView;
@end

@implementation ZXJVerityCodeLoginViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setHidden:YES];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}
- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.phoneTF.delegate = self;
    self.webView = [[WKWebViewController alloc]init];
    self.phoneTF.textColor = HexRGB(0x333333);
    self.phoneTF.keyboardType = UIKeyboardTypeNumberPad;
    if (self.type == 1 || self.type == 3) {
        if (self.type == 1) {
            self.textLab.text = @"绑定手机号";
        }else{
          self.textLab.text = @"找回密码";
        }
        self.bottomView.hidden = YES;
        [self.actionBtn setTitle:@"发送验证码" forState:(UIControlStateNormal)];
    }else{
        self.textLab.text = @"验证码登录";
        self.bottomView.hidden = NO;
        [self.actionBtn setTitle:@"下一步" forState:(UIControlStateNormal)];
    }
}
- (IBAction)SenderAction:(id)sender {

    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:self.phoneTF.text forKey:@"phone"];
    if (self.type == 1) {
        //绑定手机号
        [params setObject:@"3" forKey:@"codeMode"];
        self.codeMode = @"3";
    }else{
        //验证码登录
        [params setObject:@"1" forKey:@"codeMode"];
        self.codeMode = @"1";//1
    }
   
    [LoginRequest getpwd_vercode:params :^(id  _Nullable data, ResultCode resultCode, NSError * _Nullable Error) {
          if (resultCode == SucceedCode) {
              ZXJInputVerityCodeViewController *inputVerityVC = [[ZXJInputVerityCodeViewController alloc]init];
              inputVerityVC.type = self.type;
              inputVerityVC.phone = self.phoneTF.text;
              inputVerityVC.codeMode = self.codeMode;
              inputVerityVC.codeStr = [data objectForKey:@"codeNum"];
              [[NSUserDefaults standardUserDefaults]setObject:self.phoneTF.text forKey:@"PHONENUM"];
             [self.navigationController pushViewController:inputVerityVC animated:YES];
          }
      }];
   
}
- (IBAction)agreementAction:(id)sender {
    self.webView.titleStr = @"用户协议";
    self.webView.webUrl = @"http://app.zhishen.net.cn/xsz_h5/AppHtml/App_ServiceUsage.html";
   [self.navigationController pushViewController:self.webView animated:YES];
}
- (IBAction)securityAction:(id)sender {
    self.webView.titleStr = @"隐私协议";
    self.webView.webUrl = @"http://app.zhishen.net.cn/xsz_h5/AppHtml/App_Privacy.html";
   [self.navigationController pushViewController:self.webView animated:YES];
}
#pragma mark =======textFeildDelegate========
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if ([self validateMobile:textField.text] == YES) {
        self.actionBtn.userInteractionEnabled = YES;
        [self.actionBtn setBackgroundImage:[UIImage imageNamed:@"denglu-2"] forState:(UIControlStateNormal)];
    }else{
        self.actionBtn.userInteractionEnabled = NO;
        [self.actionBtn setBackgroundImage:[UIImage imageNamed:@"denglu-1"] forState:(UIControlStateNormal)];
    }
}
#pragma mark 判断是否是手机号
-(BOOL)validateMobile:(NSString *)mobile{
    NSString * MOBILE = @"^1(3[0-9]|4[579]|5[0-35-9]|6[6]|7[0-35-9]|8[0-9]|9[89])\\d{8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    
    if (([regextestmobile evaluateWithObject:mobile] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
